// pages/gdetail/gdetail.js
import {
  getIndexGoodDetail
} from '../../apis/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    good_info: {},
    isShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setData({
      id: options.goodid
    })
    await this.getThisGoodDetail()
  },

  getThisGoodDetail: async function () {
    if (!!this.data.id) {
      const res = await getIndexGoodDetail({
        id: this.data.id
      })
      if (res.code !== 200) {
        return wx.showToast({
          title: '获取商品信息失败',
          icon: "error",
          duration: 2000
        })
      }
      if(!!res.data) {
        if(!!res.data.specimen) {
          res.data.specimen = res.data.specimen.split(',')
          const list = []
          for(let i = 0; i < res.data.specimen.length; i++) {
            if(res.data.specimen[i] !== "") list.push(res.data.specimen[i])
          } 
          res.data.specimen = list
          this.setData({
            isShow: true
          })
        }else {
          this.setData({
            isShow: false
          })
        }
        this.setData({
          good_info: res.data
        })
      }
      console.log(this.data.good_info);
    }
  }
})